package com.tianxiao.faas.application.web.corner;

import com.tianxiao.faas.api.param.corner.*;
import com.tianxiao.faas.biz.service.corner.ActivityService;
import com.tianxiao.faas.common.access.Access;
import com.tianxiao.faas.common.result.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/app/admin/bazaar/")
public class BazaarAdminController {
    @Resource
    private ActivityService activityService;

    @RequestMapping(value = "/save")
    @Access(access = {"admin"}, action = "集市创建/修改")
    public Result<Boolean> save(@RequestBody BazaarParam param) {
        activityService.save(param);
        return Result.builder().success(true).data(true);
    }

    @RequestMapping(value = "/scan")
    @Access(access = {"admin"}, action = "集市创建/修改")
    public Result<Boolean> scan(@RequestBody ScanParam param) {
        if (param.getId() == null) {
            return Result.builder().success(false).msg("更新集市id 不能未空");
        }
        BazaarParam bazaarParam = new BazaarParam();
        bazaarParam.setId(param.getId());
        bazaarParam.setScan(param.getUrl());
        activityService.save(bazaarParam);
        return Result.builder().success(true).data(true);
    }

    @RequestMapping(value = "/list")
    @Access(access = {"admin"})
    public Result<List<BazaarResult>> list(@RequestBody BasePageParam param) {
        List<BazaarResult> results = activityService.list(param, null);
        Integer count = activityService.count(param, null);
        return Result.builder().success(true).data(results).totalCount(count).pageNo(param.getPage())
                .pageSize(param.getPageSize()).totalPage();
    }

    @RequestMapping(value = "apply/list")
    @Access(access = {"admin"})
    public Result<List<BazaarApplyResult>> listApplyShop(@RequestBody BazaarApplyPageParam param) {
        return activityService.pageShopByBazaarId(param);
    }

    @RequestMapping(value = "apply/audit")
    @Access(access = {"admin"}, action = "集市审核")
    public Result<Boolean> audit(@RequestParam("id") Long id, @RequestParam("pass") Integer pass) {
        activityService.audit(id, pass);
        return Result.builder().data(true).success(true);
    }

    @RequestMapping(value = "/delete")
    @Access(access = {"admin"})
    public Result<Boolean> delete(Long id) {
        activityService.delete(id);
        return Result.builder().success(true).data(true);
    }


    @RequestMapping(value = "/detail/get")
    @Access(access = {"admin"}, action = "集市删除")
    public Result<List<BazaarResult>> get(Long id) {
        BazaarResult bazaarResult = activityService.get(id, null);
        return Result.builder().success(true).data(bazaarResult);
    }

    @RequestMapping(value = "/detail/update")
    @Access(access = {"admin"}, action = "集市详情更新")
    public Result<Boolean> updateDetail(@RequestBody BazaarParam bazaarParam) {
        if (bazaarParam.getId() == null) {
            return Result.builder().success(false).msg("更新集市id 不能未空");
        }
        activityService.save(bazaarParam);
        return Result.builder().success(true).data(true);
    }
}
